package yl.hs.bmipfull.service.material.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.IRawMaterialCateDao;
import yl.hs.bmipfull.pojo.material.RawMaterialCateEntity;
import yl.hs.bmipfull.pojo.salemanage.CustomerInfoEntity;
import yl.hs.bmipfull.service.material.IRawMaterialCateService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  原材分类信息服务层接口实现<br/>
 * 创建时间: 2021年05月17日 08时49分39秒 <br/>
 *
 * @author 张凯
 */
@Service
public class RawMaterialCateServiceImpl extends PurchaseBaseService implements IRawMaterialCateService {

    @Autowired
    IRawMaterialCateDao dao;

    @Override
    public List<RawMaterialCateEntity> querySelect() {
        return dao.querySelect();
    }

    @Override
    public List<RawMaterialCateEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(RawMaterialCateEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), RawMaterialCateEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public RawMaterialCateEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveRawMaterialCate(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        RawMaterialCateEntity entity = new RawMaterialCateEntity();
        var user = HsApp.getCurrent();
        if (Tl.isEmpty(sn)) {
            //执行保存
            var changelog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(RawMaterialCateEntity.class));
            entity.setOrganizationCode(getDepartSN());
            var cateEntity = dao.queryByName(entity.getCateName(),null);
            if (cateEntity != null) {
                result = -1;
            } else {
                result = dao.insertNew(entity);
                if (result > 0) {
                    writeLog(ActionType.CREATE, entity.getSn(), changelog, Tl.toJson(entity), RawMaterialCateEntity.class);
                }
            }
        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setOrganizationCode(user.getOrganizationCode());
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新原材分类信息信息", Tl.toJson(entity), RawMaterialCateEntity.class);
            }
        }
        if (result > 0) {
            saveGS(
                    entity.getSn(),
                    entity.getCateName(),
                    String.join(",",
                            entity.getCateName(),
                            entity.getUseProducts()
                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    entity.getOrganizationCode()
            );
        }
        return result;
    }

    @Override
    public List<RawMaterialCateEntity> findList(String productSN) {
        return dao.findList(productSN, getCompanySNList());
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {
        var result = dao.changeStateBySn(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, RawMaterialCateEntity.class);
        }
        return result;
    }

    @Override
    public Map<String, Object> getAllCateMap() {
        var obj = new HashMap<String, Object>();
        var cateList = findList(null);
        if (cateList.size() > 0) {
            for (int i = 0; i < cateList.size(); i++) {
                var cate = cateList.get(i);
                if (!obj.containsKey(cate.getSn())) {
                    obj.put(cate.getSn(), cate.getCateName());
                }
            }
        }
        return obj;
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result = dao.changeStateBySn(snArray, state);
        if (result > 0) {
            forWriteLog(snArray, state);
        }
        return result;
    }

    @Override
    public Boolean queryByName(String cateName,String sn) {
        var  Entity = dao.queryByName(cateName,sn);
        if (Entity == null){
            return true;
        }else {
            return false;
        }
    }
}